package com.shigu.pushMessage.service;

import com.gexin.fastjson.JSON;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.dto.GtReq;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;

import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.shigu.pushMessage.bo.Payload;
import com.shigu.pushMessage.bo.PushMsgVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class AppPush {

    private static final Logger logger = LoggerFactory.getLogger(AppPush.class);

    @Value("${push.appId}")
    private String appId;
    @Value("${push.appKey}")
    private String appKey;
    @Value("${push.masterSecret}")
    private String masterSecret;
    @Value("${push.url}")
    private String url;
    @Value("${push.pack}")
    private String pack;

    public void sendToSingle(String cid,String title, String text,String payload) {
        IGtPush push = new IGtPush(url, appKey, masterSecret);

        TransmissionTemplate template = sendTemplate(title,text,payload);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);

        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            logger.error(ret.getResponse().toString());
        } else {
            logger.error("推送服务器无响应");
        }
    }

    public TransmissionTemplate sendTemplate(String title, String text,String payload) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);

        PushMsgVo vo = new PushMsgVo();
        Payload load = new Payload(payload);
        vo.setContent(text);
        vo.setTitle(title);
        vo.setPayload(load);
        template.setTransmissionContent(JSON.toJSONString(vo));
        template.setTransmissionType(2);
        Notify notify = new Notify();
        notify.setTitle(title);
        notify.setContent(text);

        notify.setIntent("intent:#Intent;" +
                "action=android.intent.action.oppopush;" +
                "launchFlags=0x14000000;" +
                "component="+pack+"/io.dcloud.PandoraEntry;" +
                "S.UP-OL-SU=true;" +
                "S.title="+title+";" +
                "S.content="+text+";" +
                "S.payload="+JSON.toJSONString(load)+";" +
                "end");
        notify.setType(GtReq.NotifyInfo.Type._intent);

        template.set3rdNotifyInfo(notify);//设置第三方通知

        return template;
    }

}

